package com.lh.music.fragment.discover;


import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.lh.music.activity.PaihanDetailActivity;
import com.lh.music.R;
import com.lh.music.adapter.PaihangAdapter;
import com.lh.music.api.Api;
import com.lh.music.callback.CallBack;
import com.lh.music.datamanager.DataManager;
import com.lh.music.entity.PaihangEntity;
import com.lh.music.util.RequestUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class PaihangFragment extends BaseFragment implements CallBack{
    private View view;
    private ListView listView;
    private List<String> types;
    private List<PaihangEntity> mData=new ArrayList<>();
    private RequestUtil requestUtil;
    PaihangAdapter paihangAdapter;
    private int count=0;
    public PaihangFragment() {
        // Required empty public constructor
    }
    private boolean isFirst=true;
    @Override
    protected void lazyLoad() {
        if (isVisiable&&isPrepare&&isFirst){
            initData();
            isFirst=false;
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view==null){
            isPrepare=true;
            view=inflater.inflate(R.layout.fragment_paihang, container, false);
            listView= (ListView) view.findViewById(R.id.listView);
            requestUtil=new RequestUtil(getActivity(),this);
            paihangAdapter=new PaihangAdapter(getActivity(),mData);
            listView.setAdapter(paihangAdapter);
            initLoadView();
            //对listview单击item的监听
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent intent=new Intent(getActivity(), PaihanDetailActivity.class);
                    intent.putExtra("type",mData.get(position).getTypeNo());
                    startActivity(intent);
                }
            });
            lazyLoad();
        }
        return view;
    }
    public void initData(){
        types= DataManager.getPaihangTypes();
        //排行有多少种类型，就进行多少次网络访问
        for (int i=0;i<types.size();i++){
            requestUtil.getPaihangContent(Api.PAIHANG_API+types.get(i));
        }
    }

    @Override
    public void callBack(String json) {
        hiddenLoad();
        try {
            JSONObject job=new JSONObject(json);
            JSONObject billboard=job.getJSONObject("billboard");
            JSONArray array=job.getJSONArray("song_list");
            PaihangEntity paihangEntity=new PaihangEntity();
            paihangEntity.setTypeNo(billboard.getString("billboard_type"));
            paihangEntity.setType(billboard.optString("name"));
            paihangEntity.setPicUrl(billboard.getString("pic_s260"));
            for (int i=0;i<3;i++){
                JSONObject result=array.getJSONObject(i);
                switch (i){
                    case 0:
                        paihangEntity.setName1(result.getString("title")+"-"+result.getString("author"));
                        break;
                    case 1:
                        paihangEntity.setName2(result.getString("title")+"-"+result.getString("author"));
                        break;
                    case 2:
                        paihangEntity.setName3(result.getString("title")+"-"+result.getString("author"));
                        break;
                }
            }
            mData.add(paihangEntity);
            count++;
            paihangAdapter.notifyDataSetChanged();

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    @Override
    public void errorCallBack() {
        hiddenLoad();
    }

    /**
     * 正在加载的初始化
     */
    public void initLoadView(){
        //baseView=LayoutInflater.from(context).inflate(R.layout.fragment_baseragment, null, false);
        ImageView iv= (ImageView) view.findViewById(R.id.load);
        AnimationDrawable anim= (AnimationDrawable) getActivity().getResources().getDrawable(R.drawable.loading_anim);
        iv.setBackground(anim);
        anim.start();
    }
    public void hiddenLoad(){
        view.findViewById(R.id.load_container).setVisibility(View.GONE);
    }
}
